package br.com.sgdc.view.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;

import javax.swing.JOptionPane;

import br.com.sgdc.Facade;
import br.com.sgdc.view.WindowAlteraSenha;
import br.com.sgdc.view.WindowEsqueceuSenha;

public class ActionConfirmarButtonWindowEsqueceuSenha implements ActionListener {
	private WindowEsqueceuSenha windowEsqueceuSenha;
	private Facade facade = new Facade();

	public ActionConfirmarButtonWindowEsqueceuSenha(WindowEsqueceuSenha windowEsqueceuSenha) {
		this.windowEsqueceuSenha = windowEsqueceuSenha;
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if(event.getSource() == windowEsqueceuSenha.confirmarButton){
			try {
				String respostaInformada = windowEsqueceuSenha.respostaTextField.getText();
				
				facade.setConfiguracaoSistema(facade.getConfiguracaoSistemaDao().listaConfiguracaoSistema());
				String respostaNoBanco = facade.getConfiguracaoSistema().getResposta();
				
				if(respostaInformada.equals(respostaNoBanco)){
					windowEsqueceuSenha.window.dispose();
					
					WindowAlteraSenha windowsAlteraSenha = new WindowAlteraSenha();
					windowsAlteraSenha.initComponentesJanela();
				} else {
					JOptionPane.showMessageDialog(null,"Resposta Incorreta!","SGDC", 2);		
				}
				
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
